package com.zzy.chapter3.deprecated.state;

import com.zzy.chapter3.utils.RedisCommonProcessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class DeprecatedSendOrder extends DeprecatedAbstractOrderState {

    @Autowired
    private RedisCommonProcessor redisCommonProcessor;

    @Autowired
    private DeprecatedAbstractOrderState orderState;

    @Override
    protected DeprecatedOrder sendOrder(String orderId) {
        // 1. redis中获取订单信息
        DeprecatedOrder order = (DeprecatedOrder) redisCommonProcessor.get(orderId);
        if(!order.getState().equals(ORDER_WAIT_SEND)) {
            throw new RuntimeException("订单状态异常");
        }
        // 2. 设置订单状态为待支付，更新redis
        order.setState(ORDER_WAIT_RECEIVE);
        redisCommonProcessor.set(orderId, order);
        // 3. 发送订单发货的event

        // 4. 设置上下文状态为“待收货”
        orderState.notifyObserve(orderId, ORDER_WAIT_RECEIVE);
        return order;
    }
}
